package com.ruoyi.labdanger.service.impl;

import java.util.List;

import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.labdanger.domain.LabApplyOccupation;
import com.ruoyi.labdanger.domain.LabApplyOccupationDeliver;
import com.ruoyi.labdanger.service.ILabApplyOccupationDeliverService;
import com.ruoyi.labdanger.service.ILabApplyOccupationService;
import com.ruoyi.system.domain.LabProductCas;
import com.ruoyi.system.service.ILabProductCasService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.labdanger.domain.LabApplyOccupationDetails;
import com.ruoyi.labdanger.domain.LabApplyPurchaseDetails;
import com.ruoyi.labdanger.mapper.LabApplyOccupationDetailsMapper;
import com.ruoyi.labdanger.service.ILabApplyOccupationDetailsService;
import com.ruoyi.labdanger.service.ILabApplyPurchaseDetailsService;
import com.ruoyi.labdata.domain.LabFlowInstance;
import com.ruoyi.labdata.service.ILabFlowInstanceService;

/**
 * 申请领用明细Service业务层处理
 * 
 * @author ruoyi
 * @date 2022-03-20
 */
@Service
public class LabApplyOccupationDetailsServiceImpl implements ILabApplyOccupationDetailsService {
	@Autowired
	private ILabApplyOccupationService labApplyOccupationService;

	@Autowired
	private LabApplyOccupationDetailsMapper labApplyOccupationDetailsMapper;

	@Autowired
	private ILabApplyPurchaseDetailsService labApplyPurchaseDetailsService;

	@Autowired
	private ILabApplyOccupationDeliverService labApplyOccupationDeliverService;

	@Autowired
	private ILabProductCasService labProductCasService;
	
	@Autowired
	private ILabFlowInstanceService labFlowInstanceService;

	/**
	 * 查询申请领用明细
	 * 
	 * @param occupationDetailsId
	 *            申请领用明细ID
	 * @return 申请领用明细
	 */
	@Override
	public LabApplyOccupationDetails selectLabApplyOccupationDetailsById(Long occupationDetailsId) {
		return labApplyOccupationDetailsMapper.selectLabApplyOccupationDetailsById(occupationDetailsId);
	}

	/**
	 * 查询申请领用明细列表
	 * 
	 * @param labApplyOccupationDetails
	 *            申请领用明细
	 * @return 申请领用明细
	 */
	@Override
	public List<LabApplyOccupationDetails> selectLabApplyOccupationDetailsList(
			LabApplyOccupationDetails labApplyOccupationDetails) {
		return labApplyOccupationDetailsMapper.selectLabApplyOccupationDetailsList(labApplyOccupationDetails);
	}

	/**
	 * 新增申请领用明细
	 * 
	 * @param labApplyOccupationDetails
	 *            申请领用明细
	 * @return 结果
	 */
	@Override
	@Transactional
	public int insertLabApplyOccupationDetails(LabApplyOccupationDetails labApplyOccupationDetails) {
		LabApplyPurchaseDetails purchaseDetails = new LabApplyPurchaseDetails();
		purchaseDetails.setPurchaseDetailId(labApplyOccupationDetails.getPurchaseDetailId());
		purchaseDetails.setScanoutNum(labApplyOccupationDetails.getOccupationQuantity());
		int count = labApplyPurchaseDetailsService.scanoutIventory(purchaseDetails);
		if (count == 0) {
			throw new RuntimeException("库存剩余数量已不够.");
		}
		labApplyOccupationDetails.setCreateBy(SecurityUtils.getUsername());
		labApplyOccupationDetails.setCreateTime(DateUtils.getNowDate());
		labApplyOccupationDetails.setStatus(0);
		return labApplyOccupationDetailsMapper.insertLabApplyOccupationDetails(labApplyOccupationDetails);
	}

	/**
	 * 修改申请领用明细
	 * 
	 * @param labApplyOccupationDetails
	 *            申请领用明细
	 * @return 结果
	 */
	@Override
	public int updateLabApplyOccupationDetails(LabApplyOccupationDetails labApplyOccupationDetails) {
		return labApplyOccupationDetailsMapper.updateLabApplyOccupationDetails(labApplyOccupationDetails);
	}

	/**
	 * 批量删除申请领用明细
	 * 
	 * @param occupationDetailsIds
	 *            需要删除的申请领用明细ID
	 * @return 结果
	 */
	/*@Override
	public int deleteLabApplyOccupationDetailsByIds(Long[] occupationDetailsIds) {
		return labApplyOccupationDetailsMapper.deleteLabApplyOccupationDetailsByIds(occupationDetailsIds);
	}*/

	/**
	 * 删除申请领用明细信息
	 * 
	 * @param occupationDetailsId
	 *            申请领用明细ID
	 * @return 结果
	 */
	@Override
	public int deleteLabApplyOccupationDetailsById(Long occupationDetailsId) {
		LabApplyOccupationDetails labApplyOccupationDetails = labApplyOccupationDetailsMapper.selectLabApplyOccupationDetailsById(occupationDetailsId);
		LabApplyPurchaseDetails purchaseDetails = new LabApplyPurchaseDetails();
		purchaseDetails.setPurchaseDetailId(labApplyOccupationDetails.getPurchaseDetailId());
		purchaseDetails.setScaninNum(labApplyOccupationDetails.getOccupationQuantity());
		int count = labApplyPurchaseDetailsService.scaninIventory(purchaseDetails);
		if (count == 0) {
			throw new RuntimeException("库存恢复失败，该库存不存在或者更新失败，库存labApplyOccupationDetails ID:"+labApplyOccupationDetails.getPurchaseDetailId());
		}
		return labApplyOccupationDetailsMapper.deleteLabApplyOccupationDetailsById(occupationDetailsId);
	}

	@Override
	@Transactional
	public int deliver(LabApplyOccupationDetails occupationDetails) {

		Long id = occupationDetails.getOccupationDetailsId();
		List<LabApplyOccupationDeliver> deliverList = occupationDetails.getDeliverList();
		LabApplyOccupationDetails applyOccupationDetails = labApplyOccupationDetailsMapper
				.selectLabApplyOccupationDetailsById(id);
		LabApplyOccupation labApplyOccupation = labApplyOccupationService
				.selectLabApplyOccupationById(applyOccupationDetails.getInstanceId());
		checkReceiver(labApplyOccupation, occupationDetails);
		LabApplyPurchaseDetails labPurchaseDetail = this.labApplyPurchaseDetailsService
				.selectLabApplyPurchaseDetailsById(applyOccupationDetails.getPurchaseDetailId());
		for (LabApplyOccupationDeliver deliver : deliverList) {
			deliver.setOccupationDetailsId(id);
			deliver.setCreateBy(SecurityUtils.getUsername());
			deliver.setCreateTime(DateUtils.getNowDate());
			labApplyOccupationDeliverService.insertLabApplyOccupationDeliver(deliver);

			LabProductCas labProductCas = new LabProductCas();
			labProductCas.setCasNo(applyOccupationDetails.getCasNo());
			labProductCas.setApplyTeacher(labApplyOccupation.getApplyTeacherNo());
			labProductCas.setCreateBy(SecurityUtils.getUsername());
			labProductCas.setCreateTime(DateUtils.getNowDate());
			labProductCas.setHazardousOpt(applyOccupationDetails.getHazardousOpt());
			labProductCas.setInventoryNum(labPurchaseDetail.getRealWeight());
			labProductCas.setQuantity(Long.valueOf(labPurchaseDetail.getQuantity()));
			labProductCas.setPackageWeight(labPurchaseDetail.getPackageWeight());
			labProductCas.setMaterialName(applyOccupationDetails.getMaterialName());
			labProductCas.setModel(applyOccupationDetails.getModel());
			labProductCas.setStorageTime(DateUtils.getTime());
			labProductCas.setUnitOpt(applyOccupationDetails.getUnitOpt());
			labProductCas.setOrgId(labApplyOccupation.getLabId());
			labProductCas.setQrCode(deliver.getQrCode());
			labProductCas.setPurchaseDetailId(labPurchaseDetail.getPurchaseDetailId());
			labProductCasService.insertLabProductCas(labProductCas);
		}

		applyOccupationDetails.setReceiver1No(occupationDetails.getReceiver1No());
		applyOccupationDetails.setReceiver1Name(occupationDetails.getReceiver1Name());
		applyOccupationDetails.setReceiver2No(occupationDetails.getReceiver2No());
		applyOccupationDetails.setReceiver2Name(occupationDetails.getReceiver2Name());
		applyOccupationDetails.setStatus(2);
		applyOccupationDetails.setReceiverTime(DateUtils.getNowDate());
		return labApplyOccupationDetailsMapper.updateLabApplyOccupationDetails(applyOccupationDetails);
	}

	private void checkReceiver(LabApplyOccupation labApplyOccupation, LabApplyOccupationDetails occupationDetails) {
		String entrusted1No = labApplyOccupation.getEntrusted1No();
		String entrusted2No = labApplyOccupation.getEntrusted2No();

		String receiver1No = occupationDetails.getReceiver1No();
		String receiver2No = occupationDetails.getReceiver2No();
		if (!StringUtils.equals(entrusted1No, receiver1No) && !StringUtils.equals(entrusted2No, receiver1No)) {
			throw new RuntimeException("学（工）号：" + receiver1No + " 不在委托领取名单中");
		}
		if (!StringUtils.equals(entrusted1No, receiver2No) && !StringUtils.equals(entrusted2No, receiver2No)) {
			throw new RuntimeException("学（工）号：" + receiver2No + " 不在委托领取名单中");
		}
	}
	
	public int finishUpdateStatus(Long instanceId){
		LabFlowInstance flowInstance = labFlowInstanceService.selectLabFlowInstanceById(instanceId);
		if(flowInstance.getFlowStatusOpt().equals("finished")){
			return labApplyOccupationDetailsMapper.finishUpdateStatus(instanceId);
		}
		return 1;
	}
	
	public List<LabApplyOccupationDetails>  queryLabApplyOccupationDetailsList(LabApplyOccupationDetails labApplyOccupationDetails){
		return labApplyOccupationDetailsMapper.queryLabApplyOccupationDetailsList(labApplyOccupationDetails);
	}
}
